<?php
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/9/27
 * Time: 19:32
 */

namespace app\modules\mch\models;

use app\models\Dynamic;

/**
 * @property Topic $model
 */
class DynamicEditForm extends MchModel
{
    public $model;

    public $store_id;
    public $title;
    public $sub_title;
    public $sort;
    public $goods_id;
    public $is_chosen;
	public $pic_list;
	public $url;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title','sub_title','is_chosen','pic_list','url'], 'required'],
            [['sort','goods_id'], 'integer','min'=>0 ,'max'=>99999999],
            [['title'], 'string', 'max' => 255],
            [['sort'], 'default', 'value' => 1000]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'store_id' => 'Store ID',
            'goods_id' => 'goods_id ID',
            'title' => '标题',
            'sub_title' => '简介',
            'sort' => '排序：升序',
            'addtime' => 'Addtime',
            'is_delete' => 'Is Delete',
            'is_chosen' => '精选',
            'pic_list' => '图片组',
            'url' => '链接',
        ];
    }

    public function save()
    {
		
        if (!$this->validate()) {
            return $this->errorResponse;
        }
        $this->model->attributes = $this->attributes;
        $this->model->store_id = $this->store_id;
        $this->model->sub_title = $this->sub_title;
        $this->model->goods_id = $this->goods_id;
        
        if ($this->model->isNewRecord) {
            $this->model->addtime = time();
        }
        if ($this->model->save()) {
            return [
                'code' => 0,
                'msg' => '保存成功',
            ];
        } else {
            return $this->getErrorResponse($this->model);
        }
    }
}
